package com.qingcheng.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SkuService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

import java.util.List;


@RestController
@RequestMapping("/elasticSearch")

@SuppressWarnings("all")
public class ElasticSearchController {


    @Reference
    private SkuService skuService;

    public void ElasticSearchInsert() throws IOException{



        //1、连接rest接口
        HttpHost http = new HttpHost("127.0.0.1",9200,"http");
        RestClientBuilder builder = RestClient.builder(http);//rest构建器
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder); //高级客户端对象


        //2、封装请求对象
        IndexRequest indexRequest = new IndexRequest("sku", "doc", "6");

        /*查询sku表数据*/
       List<Sku> skuList = skuService.findAll();


        for (Sku sku : skuList) {
            indexRequest.source(sku);
        }

        //3、获取响应结果
        IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        int status = indexResponse.status().getStatus();

        //System.out.println(status);
        System.out.println("状态"+status);
        restHighLevelClient.close();


    }





    }



